<script type="text/javascript">
$(function () {
	var url = '${contextPath}script/inv/invReturnPurchase.js';
	$.ajax({url:url, dataType:'script', cache:true,
		success: function(){
		}
	});
	$('.add-tag-input').keydown(function(e){
		  if (e.keyCode==13){
			  loadCommodity($(this));
			  this.value = "";
		  }
	});
});

// 加载商品
function loadCommodity($obj) {
	var serial = $obj.val();
	if ('' == serial || undefined == serial) return false;
	
	var parm = {'serial':serial};
	var url = create_url('invReturnPurchase-verify');
	$.ajax({type:'POST', url:url, data:parm, dataType:'json',
		success: function(result) {
			sys_submiting_close();
			if (null != result) {
				addRow(result);
			} else {
				sys_msg('串号不存在 ');
				$obj.focus();
			}
			
		}
	});
	
	
}

// 添加标签
function addRow (jobj) {
	 // 判断页面是否重复
	 var v = jobj.serial; 
	 var ok = false;
	$('#ListBody').find('input.serial').each(function(e) {
		var b = $(this).val(); 
		if (v === b) {
			sys_msg('序列号重复');
			ok = true;
			return;
		} 
	});
	if (ok) {
		return false;
	} 
	
	// 判断序列号状态 1.没有序列号，2.同类序列号， 3.唯一序列号
	if (jobj.snc == 2) {
		var comm = null;
		$('#ListBody').find('input.serial').each(function(e) {
			var b = $(this).val(); 
			if (v === b) {
				comm = $(this);
				return false;
			} 
		});
		if (null != comm) {
			sys_msg('同类序列号请直接填写数量！');
			return false;
		 }else {
			// 没有找到有相同的商品编号 新增一行 
				var tb = $('#tempTable');
				var i = parseInt(tb.attr('i'));
				
				var row = $('#tempTable').find('.rep').clone(true);
				var serialRow = $('#tempTable').find('.rep').next().clone(true);
				var serialdiv = $('#tempDiv').find('div.div-rep').clone(true);
				var j = parseInt(serialRow.attr('j'));
				row.find('.cname').attr({'id':'cname_' + i, 'name':'itemList[' + i + '].cname',"value": jobj.cname}).end()
					.find('.cc').attr({'id': 'cc_' + i, 'name': 'itemList[' + i + '].cc',"value": jobj.cc}).end()
					.find('.snc').attr({'id':'snc_' + i, 'name':'itemList[' + i + '].snc',"value": jobj.snc}).end()
					.find('.s').attr({'id': 's_' + i,"value": jobj.s}).end()
					.find('.count').attr({'i' : i,'id' : 'count_' + i,'name': 'itemList[' + i + '].count'}).end()
					.find('.price').attr({'id' : 'p_' + i,'name': 'itemList[' + i + '].price',"value": jobj.price}).end()
					.find('.to').attr('id' , 'to_' + i).end()
					.find('.dname').attr({'id': 'dname_' + i,"value": jobj.dname}).end()
					.find('.dc').attr({'id': 'dc_' + i, 'name': 'itemList[' + i + '].dc',"value": jobj.dc}).end();
					
				serialdiv.find('input.serial').attr({'i' : i,'id': 't_' + j, 'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": v}).end()
						.find('.del-tag').attr({'onclick':'javascript: delete_invReturnPurchase($(this),' + j + ');'}).end()
						.find('a.show-serial').html(v);
				
				
				
				
				row.attr('i', i);
				tb.attr('i', ++i);
				serialRow.attr('j', '1');
				$(serialRow.children()[0]).append(serialdiv);
				$('#ListBody').append(row);
				$('#ListBody').append(serialRow);
				price_invReturnPurchase();
		}
		
	} else if (jobj.snc == 3) {
		
		var s = jobj.cc;
		var comm = null;
		$('#ListBody').find('input.cc').each(function(e) {
			var b = $(this).val(); 
			if (b == s) {
				// 找到商品编号相同的input
				comm = $(this);
				return false;	// 跳出each 循环
			}
		});
		
		 if (null != comm) {
			// 找到同样商品编号的 直接把序列号添加到 序列号的地方
			var tempRep = comm.parents('tr.rep');	// 找对应行的 放序列号的TD
			var serialDiv = $('#tempTable').next().find('div.div-rep').clone(true);	// 序列号
			var tb = $('#tempTable');
			var i = parseInt(tb.attr('i') - 1);
			var j = tempRep.next().attr("j");
			serialDiv.find('input.serial').attr({'i' : i, 'id': 't_' + j, 'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": v}).end()
					.find('.del-tag').attr({'onclick':'javascript: delete_invReturnPurchase($(this),' + j + ');'}).end()
					.find('a.show-serial').html(v);
			
			
			
			
			tempRep.next().attr('j', ++j);
			 $(tempRep.next().children()[0]).append(serialDiv);
			 
			var count = parseInt(tempRep.find('input.count').val());
			if ('' != count && undefined != count)
				count++;

			tempRep.find('input.count').val(count);
			price_invReturnPurchase();
			 //num_invReturnPurchase(i);
			 
		 } else {
			// 没有找到有相同的商品编号 新增一行 
			var row = $('#tempTable').find('.rep').clone(true);
			var serialRow = $('#tempTable').find('.rep').next().clone(true);
			var serialdiv = $('#tempDiv').find('div.div-rep').clone(true);
			
			var tb = $('#tempTable');
			var i = parseInt(tb.attr('i'));
			
			var j = parseInt(serialRow.attr('j'));
			row.find('.cname').attr({'id':'cname_' + i, 'name':'itemList[' + i + '].cname',"value": jobj.cname}).end()
				.find('.cc').attr({'id': 'cc_' + i, 'name': 'itemList[' + i + '].cc',"value": jobj.cc}).end()
				.find('.snc').attr({'id':'snc_' + i, 'name':'itemList[' + i + '].snc',"value": jobj.snc}).end()
				.find('.s').attr({'id': 's_' + i,"value": jobj.s}).end()
				.find('.count').attr({'i' : i,'id' : 'count_' + i,'name': 'itemList[' + i + '].count', 'value':'1', 'readonly':'readonly'}).end()
				.find('.price').attr({'id' : 'p_' + i,'name': 'itemList[' + i + '].price',"value": jobj.price}).end()
				.find('.to').attr('id' , 'to_' + i).end()
				.find('.dname').attr({'id': 'dname_' + i,"value": jobj.dname}).end()
				.find('.dc').attr({'id': 'dc_' + i, 'name': 'itemList[' + i + '].dc',"value": jobj.dc}).end();
				
			serialdiv.find('input.serial').attr({'i' : i,'id': 't_' + j, 'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": v}).end()
					.find('.del-tag').attr({'onclick':'javascript: delete_invReturnPurchase($(this),' + j + ');'}).end()
					.find('a.show-serial').html(v);
			
			
			row.attr('i', i);
			tb.attr('i', ++i);
			
			
			serialRow.attr('j', '1');
			$(serialRow.children()[0]).append(serialdiv);
			$('#ListBody').append(row);
			$('#ListBody').append(serialRow);
			//num_invReturnPurchase(i - 1);
			price_invReturnPurchase();
		 }
	}
}


</script>

<style>
.my-filter-table {
	background: linear-gradient(to bottom, #F1FAFE 0%, #D9EDF7 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
	border: 1px solid #9BE2F0;
	display: inline-block;
	margin-bottom: 5px;
	margin-left: 5px;
	text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
}
.my-filter-table a {
	color: #3A87AD;
    display: inline-block;
    margin: 0 4px;
}

.my-filter-table button {
	background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
    border: 0 none;
    cursor: pointer;
    padding: 0;
    color: #000000;
    float: right;
    font-size: 20px;
    font-weight: bold;
    line-height: 20px;
    opacity: 0.4;
    text-shadow: 0 1px 0 #FFFFFF;
}

.add-tag-input {
	width:120px;
}
</style>
<form id="editForm">
	<table class="table table-bordered animate2 fadeInUp">
		<tr>	
			<td><font>请输入序列号&nbsp;&nbsp;&nbsp;</font><input type="text" class="add-tag-input" /></td>
		</tr>
	</table>

	<table class="table table-bordered animate2 fadeInUp">
		<thead>
			<tr height="40">
				<th>商品名称<a href="javascript:;" onclick="javascript:add_invReturnPurchase();return false;" style="margin-left: 10px">[添加空白行]</a></th>
				<th>序列号状态</th>
				<th>数量</th>
				<th>单价</th>
				<th>仓库</th>
				<th style="width:32px;">操作</th>
			</tr>
		</thead>
		<tbody id="ListBody">
		</tbody>
		<tr>
			<td colspan="3">退款额:&nbsp;&nbsp;&nbsp;<input id="total" name="total" value="${(purchase.total)!''}" type="text"/></td>
			<td colspan="3">来往商名称:&nbsp;&nbsp;
				<div class="input-append">
					<input class="bn" id="bname" name="bname" type="text" style="width:150px;" value="${(purchase.bname)!''}" readonly/>
					<input id="bc" name="bc" value="${(purchase.bc)!''}" type="hidden" />
					<button class="btn btn-xs btn-primary" type="button" onclick="javascript:selectBn_invReturnPurchase($(this));return false;"><i class="icon-search"></i></button>
				</div>
			</td>
		</tr>
		<tr>
			<td colspan="6">退货原因:&nbsp;&nbsp;<textarea id="remark" name="remark" value="${(purchase.remark)!''}"/></td>
		</tr>
					
		<tr height="40">
			<td colspan="7" style="text-align: center;">
				<a href="javascript:;" class="btn btn-info" onclick="javascript: save_invReturnPurchase(); return false;"><i class="icon-save"></i>&nbsp;保&nbsp;&nbsp;存</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<a href="javascript:;" class="btn btn-primary" onclick="javascript: sys_load_handle('invReturnPurchase');return false;"><i class="fontello-icon-block"></i>&nbsp;取&nbsp;&nbsp;消</a>
			</td>
		</tr>
	</table>
</form>




<table style="display:none;" id="tempTable" i="0">
	<tr class="rep">
		<td>
			<input class="cname" type="text" style="width:120px;" readonly />
			<button class="btn btn-xs btn-primary" type="button" style="display:none;"><i class="icon-search"></i></button>
			<input class="cc" type="hidden" />
		</td>
		<td><input class="snc" style="width:100px;" type="hidden" readonly/><span class="s"></span></td>
		<td><input class="count" type="text" style="width:60px;" onchange="javascript:price_invReturnPurchase($(this));return false;" onkeyup="javascript:checkNum_invReturnPurchase($(this));return false;"/></td>
		<td><input class="price" type="text" style="width:80px;" readonly/></td>
		<input class="to" type="hidden"/>
		<td>
			<input class="dname" type="text" style="width:120px;" readonly />
			<input class="dc" type="hidden" />
		</td>
		<td style="text-align:center;"><a class="btn btn-danger" href="javascript:;" onclick="javascript:delete_invReturnPurchase($(this));return false;"><i class="icon-remove"></i></a></td>
	</tr>
	<tr j="0">
		<td colspan="7">
			
		</td>
	</tr>
</table>
<div style="display:none;" id="tempDiv">
	<div class="my-filter-table div-rep">
		<input class="serial form-control" type="hidden" style="width:60px;" />
		<a class="show-serial" href="javascript:;"></a>&nbsp;&nbsp;&nbsp;&nbsp;
		<button type="button" class="del-tag" data-dismiss="alert">&times;</button>
	</div>
</div>